package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import bean.ConfigBean;

public class ParseXmlConfig {
	
	public static Map<String, Object> configMap = new HashMap<String, Object>(); 
	
	public static void parseStrutsConfig(String xmlPath){
		    try {  
		        //创建SAX建造者对象，该类构造方法的重载boolean类型的方法中validate表示是否验证xml文档。  
		        SAXBuilder saxBuilder = new SAXBuilder(false);  
		        InputStream inputStream = new FileInputStream(new File(xmlPath));    
		        //解析xml文档，返回document文档对象  
		        Document document = saxBuilder.build(inputStream);  
		        Element rootElement = document.getRootElement();//根节点  
		        
		        //解析formbean
		        parseFromBean(rootElement);
		        
		        //解析action
		        parseActions(rootElement);
		       
		        
		        //解析
		    } catch (JDOMException e) {  
		        e.printStackTrace();  
		    } catch (IOException e) {  
		        e.printStackTrace();  
		    }    
	}
	
	
	/**
	 * 解析formBean
	 * @param rootElement
	 */
	@SuppressWarnings("unchecked")
	public static void parseFromBean(Element rootElement){
		 //解析formbean
        Element formBeansElement = rootElement.getChild("formBeans");  
        if(formBeansElement != null){
        	List<Element> formBeanList = formBeansElement.getChildren("formBean");
        	for(Element formBean : formBeanList){
        		configMap.put(formBean.getAttributeValue("name"), 
        							formBean.getAttributeValue("class"));
        	}
        }else{
        	throw new RuntimeException("未定义formBeans");
        }
	}
	
	
	/**
	 * 解析actions
	 * @param rootElement
	 */
	@SuppressWarnings("unchecked")
	public static void parseActions(Element rootElement){
        Element actionsElement = rootElement.getChild("actions");  
        if(actionsElement != null){
        	List<Element> actionList = actionsElement.getChildren("action");
        	for(Element action : actionList){
        		ConfigBean cb = new ConfigBean();
        		
        		cb.setBeanName(action.getAttributeValue("beanName"));
        		cb.setBeanClass(configMap.get(action.getAttributeValue("beanName")).toString());
        		cb.setActionClass(action.getAttributeValue("class"));
        		cb.setActionPath(action.getAttributeValue("path"));
        		
        		List<Element> forwardList = action.getChildren("forward");
        		for(Element forward : forwardList){
        			
        			cb.getForwardMap().put(forward.getAttributeValue("name"), 
        													forward.getTextTrim());
        		}
        		
        		configMap.put(cb.getActionPath(), cb);
        	}
        }else{
        	throw new RuntimeException("未定义actions");
        }
	}
	
	public static void main(String[] args) {
		ParseXmlConfig p = new ParseXmlConfig();
		//p.parseStrutsConfig();
	}

}
